/**
 * SKU库存预加载功能测试
 * 用于验证预加载逻辑是否正确工作
 */

// 模拟SKU数据
const mockSkus = [
    {
        id: 1,
        specs: "红色@大码",
        price: 99.00,
        stock: 10
    },
    {
        id: 2,
        specs: "红色@小码",
        price: 99.00,
        stock: 0
    },
    {
        id: 3,
        specs: "蓝色@大码",
        price: 99.00,
        stock: 5
    },
    {
        id: 4,
        specs: "蓝色@小码",
        price: 99.00,
        stock: 8
    }
];

// 模拟规格数据
const mockSpecs = [
    {
        name: "颜色",
        vs: [
            { value: "红色", selected: false },
            { value: "蓝色", selected: false }
        ]
    },
    {
        name: "尺码",
        vs: [
            { value: "大码", selected: false },
            { value: "小码", selected: false }
        ]
    }
];

/**
 * 构建SKU库存映射表
 */
function buildSkuStockMap(allSkus, specs) {
    const skuStockMap = {};
    
    allSkus.forEach(sku => {
        if (sku.specs) {
            skuStockMap[sku.specs] = {
                sku: sku,
                stock: sku.stock
            };
        }
    });
    
    return skuStockMap;
}

/**
 * 检查单个规格值的库存状态
 */
function checkSpecValueStockStatus(specValue, currentSpec, allSpecs, skuStockMap) {
    let hasStock = false;
    let minStock = Infinity;
    
    Object.keys(skuStockMap).forEach(specsString => {
        const specValues = specsString.split('@');
        if (specValues.includes(specValue.value)) {
            const skuInfo = skuStockMap[specsString];
            if (skuInfo && skuInfo.stock > 0) {
                hasStock = true;
                if (skuInfo.stock < minStock) {
                    minStock = skuInfo.stock;
                }
            }
        }
    });
    
    return {
        status: hasStock ? 'normal' : 'outOfStock',
        disabled: !hasStock,
        stockInfo: { minStock: minStock === Infinity ? 0 : minStock }
    };
}

/**
 * 更新规格数据，为每个规格值添加库存状态
 */
function updateSpecsWithStockInfo(specs, skuStockMap) {
    return specs.map(spec => ({
        ...spec,
        vs: spec.vs.map(specValue => {
            const stockStatus = checkSpecValueStockStatus(specValue, spec, specs, skuStockMap);
            
            return {
                ...specValue,
                stockStatus: stockStatus.status,
                disabled: stockStatus.disabled,
                stockInfo: stockStatus.stockInfo
            };
        })
    }));
}

// 测试执行
console.log('=== SKU库存预加载功能测试 ===');

console.log('1. 原始规格数据:');
console.log(JSON.stringify(mockSpecs, null, 2));

console.log('\n2. 构建SKU库存映射表:');
const skuStockMap = buildSkuStockMap(mockSkus, mockSpecs);
console.log(JSON.stringify(skuStockMap, null, 2));

console.log('\n3. 更新规格数据，添加库存状态:');
const updatedSpecs = updateSpecsWithStockInfo(mockSpecs, skuStockMap);
console.log(JSON.stringify(updatedSpecs, null, 2));

console.log('\n4. 测试结果分析:');
updatedSpecs.forEach(spec => {
    console.log(`\n规格: ${spec.name}`);
    spec.vs.forEach(v => {
        console.log(`  ${v.value}: ${v.disabled ? '无库存(禁用)' : '有库存(可用)'} - 最小库存: ${v.stockInfo.minStock}`);
    });
});

console.log('\n=== 测试完成 ===');
